/* 
 * File:   mblaop.h
 * Author: gustavo
 *
 * Created on August 12, 2012, 4:08 AM
 */

#ifndef MBLAOP_H
#define	MBLAOP_H

/**
 * A[0:m-1][0:n-1]  += B[0:m-1][0:n-1]
 * 
 * @param m
 * @param n
 * @param A
 * @param sta
 * @param B
 * @param stb
 */
template <typename T> inline void m_add(int m, int n, T * A, int sta, const T * B, int stb) {

	int r, c;

	for (r = 0; r < m; r++) {
		for (c = 0; c < n; c++) {

			*A += *B;

			A++;
			B++;
		}
		A += (sta - n);
		B += (stb - n);
	}

}

/**
 * A[0:m-1][0:n-1]  -= B[0:m-1][0:n-1]
 * 
 * @param m
 * @param n
 * @param A
 * @param sta
 * @param B
 * @param stb
 */
template <typename T> inline void m_sub(int m, int n, T * A, int sta, const T * B, int stb) {

	int r, c;

	for (r = 0; r < m; r++) {
		for (c = 0; c < n; c++) {

			*A -= *B;

			A++;
			B++;
		}
		A += (sta - n);
		B += (stb - n);
	}

}

/**
 * A[0:m-1][0:n-1] = - B[0:m-1][0:n-1]
 * 
 * @param m
 * @param A
 * @param inca
 * @param B
 * @param incb
 */
template <typename T> inline void m_move_neg(int m, T * A, int inca, const T * B, int incb) {

}

/**
 * This function multiplies two matrices. The result is stored in M.
 * 
 * @param rows_a - number of rows of the matrix A.
 * @param cols_a - number of columns of the matrix A.
 * @param A - pointer to the first element of the matrix.
 * @param sta - stride of A. Number of elements in memory from the first row to the next.
 * @param rows_b - number of rows of the matrix B.
 * @param cols_b - number of columns of the matrix B.
 * @param B - pointer to the first element of the matrix.
 * @param stb - stride of B. Number of elements in memory from the first row to the next.
 * @param M - pointer to the first element of the product matrix.
 * @param stm - stride of M. Number of elements in memory from the first row to the next.
 */
template <typename T> void m_mult(int rows_a, int cols_a, T * A, int sta, int rows_b, int cols_b, T * B, int stb, T * M, int stm) {

	int r, c, k;

	T * _B = B;
	T * Ai;
	T * Bi;

	for (r = 0; r < rows_a; r++) {

		B = _B;

		for (c = 0; c < cols_b; c++) {

			Ai = A;
			Bi = B;
			*M = 0;

			for (k = 0; k < rows_b; k++) {
				*M += (*Ai) * (*Bi);

				Ai++;
				Bi += stb;
			}
			M++;
			B++;
		}
		M += (stm - cols_b);
		A += sta;
	}

}


#endif	/* MBLAOP_H */

